package org.jeecg.modules.sggg.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.sggg.entity.SgItem;
import org.jeecg.modules.sggg.entity.SgItemType;
import org.jeecg.modules.sggg.service.ISgItemTypeService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 项目类型
 * @Author: jeecg-boot
 * @Date:   2023-03-30
 * @Version: V1.0
 */
@Api(tags="项目类型")
@RestController
@RequestMapping("/itemType/sgItemType")
@Slf4j
public class SgItemTypeController extends JeecgController<SgItemType, ISgItemTypeService> {
	@Autowired
	private ISgItemTypeService sgItemTypeService;

	 @Autowired
	 private RedisUtil redisUtil;

	 @Autowired
	 private ISysDepartService sysDepartController;

	 @Autowired
	 private ISysUserService sysUserService;

	/**
	 * 分页列表查询
	 *
	 * @param sgItemType
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "项目类型-分页列表查询")
	@ApiOperation(value="项目类型-分页列表查询", notes="项目类型-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<SgItemType>> queryPageList(SgItemType sgItemType,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		QueryWrapper<SgItemType> queryWrapper = QueryGenerator.initQueryWrapper(sgItemType, req.getParameterMap());
		String roleCode = redisUtil.get("roleCode:" + sysUser.getId()).toString();
		SysDepart sysDepart = sysDepartController.getById(sysUser.getDepartIds());
		// 根据DeptAttr机构属性判断当前登录人属于哪个机构（三高之家、三高基地、三高中心）
		if(sysDepart !=null && sysDepart.getDeptAttr()!=null){
			SysUser sysUser1 = sysUserService.getById(sysUser.getId());
			// 1.三高之家
			if(sysDepart.getDeptAttr().equals("1")){
				queryWrapper.eq("sys_org_code", sysUser.getOrgCode().toString());
			}
			// 2.三高基地
			if(sysDepart.getDeptAttr().equals("2")){
				String[] array = sysUser1.getResponsibleAgency().split(",");
				List<String> responsibleAgencies = Arrays.asList(array);
				queryWrapper.in("sys_org_code", responsibleAgencies);
			}
			// 3.三高中心
			if(sysDepart.getDeptAttr().equals("3")){
				String[] array = sysUser1.getResponsibleAgency().split(",");
				List<String> responsibleAgencies = Arrays.asList(array);
				queryWrapper.in("sys_org_code", responsibleAgencies);
			}
		}else if(roleCode.contains("dept_manage") || roleCode.contains("admin")){
			queryWrapper.like("sys_org_code",sysUser.getOrgCode());

		}else{
			queryWrapper.eq("sys_org_code", sysUser.getOrgCode().toString());
		}
		Page<SgItemType> page = new Page<SgItemType>(pageNo, pageSize);
		IPage<SgItemType> pageList = sgItemTypeService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param sgItemType
	 * @return
	 */
	@AutoLog(value = "项目类型-添加")
	@ApiOperation(value="项目类型-添加", notes="项目类型-添加")
	//@RequiresPermissions("itemType:sg_item_type:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody SgItemType sgItemType) {
		sgItemTypeService.save(sgItemType);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param sgItemType
	 * @return
	 */
	@AutoLog(value = "项目类型-编辑")
	@ApiOperation(value="项目类型-编辑", notes="项目类型-编辑")
	//@RequiresPermissions("itemType:sg_item_type:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody SgItemType sgItemType) {
		sgItemTypeService.updateById(sgItemType);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "项目类型-通过id删除")
	@ApiOperation(value="项目类型-通过id删除", notes="项目类型-通过id删除")
	//@RequiresPermissions("itemType:sg_item_type:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		sgItemTypeService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "项目类型-批量删除")
	@ApiOperation(value="项目类型-批量删除", notes="项目类型-批量删除")
	//@RequiresPermissions("itemType:sg_item_type:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.sgItemTypeService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "项目类型-通过id查询")
	@ApiOperation(value="项目类型-通过id查询", notes="项目类型-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<SgItemType> queryById(@RequestParam(name="id",required=true) String id) {
		SgItemType sgItemType = sgItemTypeService.getById(id);
		if(sgItemType==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(sgItemType);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param sgItemType
    */
    //@RequiresPermissions("itemType:sg_item_type:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SgItemType sgItemType) {
        return super.exportXls(request, sgItemType, SgItemType.class, "项目类型");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("itemType:sg_item_type:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SgItemType.class);
    }

	 /**
	  * TODO     项目上下架
	  * @author  WangJie
	  * @date   2023/3/31 9:10
	  * @param
	  * @return
	  * @Exception
	  *
	  */
	 @AutoLog (value = "项目表-商品上下架")
	 @ApiOperation (value = "项目表-商品上下架", notes = "项目表-商品上下架")
	 @RequestMapping (value = "/isOnSale", method = {RequestMethod.PUT, RequestMethod.POST})
	 public Result<String> isOnSale (@RequestBody SgItemType sgItemType) {
		 sgItemType.setIsOnSale(sgItemType.getIsOnSale()==0?1:0);
		 sgItemTypeService.updateById(sgItemType);
		 return Result.OK(sgItemType.getIsOnSale()==0?"下架成功!":"上架成功!");
	 }
}
